﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using BaseData;
using BaseData.App;
using BaseData.Exts;
using BaseData.HelperEzui;
using BaseLib.Helper;
using BaseUtility;
using Newtonsoft.Json;
namespace WebUI.Ashx.Acc.Staff
{
    /// <summary>
    /// IndexAccordion 的摘要说明
    /// </summary>
    public class IndexAccordion : Base, IHttpHandler
    {
        private static readonly AccClass _accId = new AccExts(new GetAccType().Parse(Request.QueryString["url"])).Instances;
        public void ProcessRequest(HttpContext context)
        {
            // Response.Write("_accId.Id：" + _accId.Id + " _accId.Id：" + _accId.Name + " _accId.Id：" + _accId.Type + "::");
            if (_accId.Id == 0 || string.IsNullOrWhiteSpace(_accId.Name) || _accId.Type == 0) return;
            // var id = context.Request.QueryString["id"].ToInt(0);
            // context.Response.Write(this.GetModels(id).ObjectsToJson());
            // context.Response.ContentType = "text/plain";

            var doc = new XmlDocument();
            doc.Load(Server.MapPath("/Zone/Config/Models.xml")); //加载Xml文件
            Response.Write(new IndexAccordion().GetNode(doc.ChildNodes[0].ChildNodes, Request.QueryString["id"].ToInt(0)).ObjectsToJson());
            Response.ContentType = "text/plain";
        }
        public List<AccordionXml> GetNode(XmlNodeList nodes, int reqId, int getId = 0, int index = 0)
        {
            // 顶级外的各级
            if (reqId == 0) return this.GetNodeList(nodes, reqId, true); // 顶级
            if (getId != 0) return this.GetNodeList(nodes, reqId);
            foreach (XmlNode node in nodes)
            {
                index++;
                if (index != reqId) continue;
                return this.GetNodeList(node.ChildNodes, reqId);
            }
            return new List<AccordionXml>();
        }

        public List<AccordionXml> GetNodeList(XmlNodeList nodes, int reqId, bool top = false, int index = 0)
        {
            var list = new List<AccordionXml>();
            foreach (XmlNode node in nodes)
            {
                index++;
                if (node.Attributes == null || node.Attributes.Count == 0) continue;
                var type = node.Attributes["type"] != null ? node.Attributes["type"].Value : "";
                var display = node.Attributes["display"] == null || Convert.ToBoolean(node.Attributes["display"].Value);
                if (!display) continue;
                if (string.IsNullOrWhiteSpace(type) || type.IndexOf(value: _accId.Type.ToString(), comparisonType: System.StringComparison.Ordinal) == -1) continue;
                var expand = Convert.ToBoolean(node.Attributes["expand"] != null ? node.Attributes["expand"].Value : "true");
                var children = this.GetNode(node.ChildNodes, reqId, index);
                var state = expand || !children.Any() ? "open" : "closed";
                var nodex = new AccordionXml
                {
                    id = index,
                    text = node.Attributes["text"] != null ? node.Attributes["text"].Value : "",
                    url = node.Attributes["url"] != null ? node.Attributes["url"].Value : "",
                    state = state,
                    children = top ? null : children,
                };
                list.Add(nodex);
            }
            return list;
        }

        private List<Accordion> GetModels(int id)
        {
            var items = (ModelsIndex.Init()
                .Where(q => q.pId == id && q.list.Contains(_accId.Type) && q.Display)
                .OrderBy(q => q.SortId)).ToList();
            items.ForEach(f =>
            {
                var str = string.Empty;
                //str = "【PkId:" + UserInfoExt.PkId + " - Name:" + UserInfoExt.Name + " - UserType:" + UserInfoExt.UserTypeSystem + "】";
                f.text = id == 0 ? f.text + str : f.text;
                f.children = this.GetModels(f.id).ToList();
            });
            return items;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}